//
//  RosterViewController.m
//  VKChat
//
//  Created by Максим Бризинский on 31.01.11.
//  Copyright 2011 БГУИР. All rights reserved.
//

#import "RosterViewController.h"
#import "User.h"


@implementation RosterViewController
@synthesize selectedUid = _chosedUid;
@synthesize delegate = _delegate;

-(void)initRoster {
	_roster = [[Roster alloc] init];
	_roster.delegate = self;
	_vkApi.rosterDelegate = _roster;
	[NSThread detachNewThreadSelector:@selector(asyncInitRoster) toTarget:self withObject:nil];
}

-(void)asyncInitRoster {
	sleep(1);
	NSArray* allFriends = [_vkApi getAllFriends];
	if (allFriends != nil) {
		_roster.allFriends = allFriends;
	} else {
		NSLog(@"RosterViewController: can't get all friends");
	}
	
	sleep(1);
	NSMutableArray* onlineFriends = [[_vkApi getFriendsOnline] mutableCopy];
	if (onlineFriends != nil) {
		_roster.onlineFriends = onlineFriends;
	} else {
		NSLog(@"RosterViewController: can't get online friends");
	}
	[_friendsTable reloadData];
}

-(void)userOnline:(User*)user {
	[_friendsTable reloadData];
	_chosedUid = [_friendsTable selectedRow];
	[_delegate didChangeUserUid:_chosedUid];
	[GrowlApplicationBridge notifyWithTitle:[NSString stringWithFormat:@"%@ %@",user.firstName, user.lastName]
								description:@"is online now"
						   notificationName:@"UserOnline"
								   iconData:nil
								   priority:0
								   isSticky:NO
							   clickContext:nil];
}

-(void)userOffline:(User *)user {
	[_friendsTable reloadData];
	_chosedUid = [_friendsTable selectedRow];
	[_delegate didChangeUserUid:_chosedUid];
	[GrowlApplicationBridge notifyWithTitle:[NSString stringWithFormat:@"%@ %@",user.firstName, user.lastName]
								description:@"is offline now"
						   notificationName:@"UserOffline"
								   iconData:nil
								   priority:0
								   isSticky:NO
							   clickContext:nil];
}

-(void)awakeFromNib {
	_vkApi = [VKApi getInstance];
	[GrowlApplicationBridge setGrowlDelegate:self];
	[_friendsTable setDataSource:self];
	[_friendsTable setDelegate:self];
	_chosedUid = 0;
}

-(int)numberOfRowsInTableView:(NSTableView *)tableView {
	int num = [_roster.onlineFriends count];
	return num;
}

-(id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
	User* user = [_roster.onlineFriends objectAtIndex:row];
	NSString *name = [NSString stringWithFormat:@"%@ %@",user.firstName, user.lastName];
	return name;
}
-(void) tableViewSelectionDidChange:(NSNotification *)notification {
	int selectedRow = [_friendsTable selectedRow];
	User* user = [_roster.onlineFriends objectAtIndex:selectedRow];
	_chosedUid = user.uid;
	[_delegate didChangeUserUid:user.uid];
	NSLog(@"%lld",_chosedUid);
}
- (NSDictionary *) registrationDictionaryForGrowl { /* Only implement this method if you do not plan on just placing a plist with the same data in your app bundle (see growl documentation) */
    NSArray *array = [NSArray arrayWithObjects:@"UserOnline", @"UserOffline", nil]; /* each string represents a notification name that will be valid for us to use in alert methods */
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithInt:1], /* growl 0.7 through growl 1.1 use ticket version 1 */
                          @"TicketVersion", /* Required key in dictionary */
                          array, /* defines which notification names our application can use, we defined example and error above */
                          @"AllNotifications", /*Required key in dictionary */
                          array, /* using the same array sets all notification names on by default */
                          @"DefaultNotifications", /* Required key in dictionary */
                          nil];
    return dict;
}


@end
